// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================
package tc.map;

//=============================================================================
/**Tile addres consists from layer (zoom - 1-18), horizontal index (hor) and vertical 
 * index (ver). Number of indexes changes by layer:
 * layer 1: 2
 * layer 2: 4
 * layer 3: 8  
 * .....
 * Layer 0 exists, but it is excluded
 */
public class TcTileAddress {
//=============================================================================	
private int layer;
private int hor;
private int ver;
		
public static final int LAYER_MIN = 1;
public static final int LAYER_MAX = 18;

public TcTileAddress(int layer, int hor, int ver) {
	this.layer = layer;
	this.hor   = hor;
	this.ver   = ver;
}

//=============================================================================
public int getLayer() { return layer; }
public int getHor()   { return hor; }
public int getVer()   { return ver; }

@Override
public String toString() {
	return String.format("%d/%d/%d", layer, hor, ver);
}

public int distanceTo(TcTileAddress snd) {
	if (layer != snd.layer) return 20*Math.abs(layer-snd.layer);
	return Math.abs(hor-snd.hor) + Math.abs(ver-snd.ver);
}

public static int maxHorVer(int layer) {
	int result = 1;
	for (int i = 0; i < layer; i++ ) result *= 2;
	result -= 1;
	return result;
}
//=============================================================================
}
